<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>计算属性与侦听属性</title>
    <script src="./vue.js"></script>
</head>
<body>
    <div id="demo">侦听属性：{{ fullName }}</div>
    <div id="demo2">计算属性：{{ fullName }}</div>
    <div id="demo3">
        <p>设置fullName: {{ fullName }} 获取firstName: {{ firstName }}和lastName:   {{ lastName }}</p>
        <input v-model="fullName">
    </div>
    <script>
        var demo = new Vue({
            el: "#demo",
            data: {
                firstName: "Ioiy",
                lastName: "Aig",
                fullName: "Ioiy Aig"
            },
            // 侦听属性（watch）：更通用的观察和相应Vue实例上的数据变动
            watch: {
                firstName: function(val) {
                    this.fullName = val + " " + this.lastName;
                },
                lastName: function(val) {
                    this.fullName = this.firstName + " " + val;
                }
            }
        });
        var demo2 = new Vue({
            el: "#demo2",
            data: {
                firstName: "Ioiy",
                lastName: "Aig"
            },
            // 使用计算属性达到相同的效果（代码更加简洁）
            computed: {
                fullName: function() {
                    return this.firstName + " " + this.lastName;
                }
            }
        });
        var demo3 = new Vue({
            el: "#demo3",
            data: {
                firstName: "Ioiy",
                lastName: "Aig"
            },
            computed: {
                fullName: {
                    get: function () {
                        return  this.firstName + " " + this.lastName;
                    },
                    set: function (val) {
                        var names = val.split(" ");
                        this.firstName = names[0];
                        this.lastName = names[names.length - 1];
                    }
                }
            }
        });
    </script>
</body>
</html>